<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <!-- <van-nav-bar class="nav-bar-page" title="标题" /> -->
    <van-nav-bar class="page-nav" title="登录">
      <van-icon slot="left" name="cross" @click="goBack" />
    </van-nav-bar>
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field v-model="user.mobile" name="mobile" label="手机号" placeholder="请输入手机号"
        :rules="userFormRules.mobile">
        <template #left-icon>
          <i class="toutiao toutiao-shouji"></i>
        </template>
      </van-field>
      <van-field v-model="user.smsCode" type="password" name="验证码" label="验证码" placeholder="验证码"
        :rules="userFormRules.smsCode">
        <template #left-icon>
          <i class="toutiao toutiao-yanzhengma"></i>
        </template>
          <template #button>
            <van-button v-if="!isCountDownShow" class="send-sms-btn" round size="small" type="default" native-type="button" @click="onSendSms" >发送验证</van-button>
            <van-count-down v-else :time="30 * 1000"  format="ss 秒" @finish="isCountDownShow=false" />
          </template>
      </van-field>
      <div style="margin: 16px;">
        <van-button block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>

  </div>
</template>

<script>
import { loginApi, getSmsCodeApi } from '@/api/index.js'
export default {
  name: 'LoginIndex',
  data () {
    return {
      user: {
        mobile: '',
        smsCode: ''
      },
      userFormRules: {
        mobile: [{
          required: true,
          message: '手机号不能为空'
        }, {
          pattern: /^1[3|5|7|8]\d{9}$/,
          message: '手机号格式错误'
        }],
        smsCode: [{
          required: true,
          message: '验证码不能为空'
        }, {
          pattern: /^\d{6}$/,
          message: '验证码格式错误'
        }]
      },
      isCountDownShow: false // 倒计时开关
    }
  },
  methods: {
    // 提交表单
    async onSubmit () {
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        message: '加载中...',
        forbidClick: true
      })
      try {
        const { data: res } = await loginApi({
          mobile: this.user.mobile,
          code: this.user.smsCode
        })
        console.log(res)
        this.$toast.success('登录成功')
        this.$store.commit('setUser', res.data)
        // 登录成功跳转
        this.$router.push(this.$route.query.path || '/')
      } catch (e) {
        if (e.response.status === 400) {
          console.log('手机或验证码错误', e)
          this.$toast.fail('手机或验证码错误')
        } else {
          console.log('服务器内部错误', e)
          this.$toast.fail('服务器内部错误')
        }
      }
    },
    // 发送验证码
    async onSendSms () {
      // 验证手机输入框是否符合规则
      try {
        await this.$refs.loginForm.validate('mobile')
        this.isCountDownShow = true
      } catch (e) {
        return console.log(e)
      }
      try {
        // 发送成功时
        await getSmsCodeApi(this.user.mobile)
        this.$toast.success('发送成功')
      } catch (e) {
        // 发送失败时
        console.log(e)
        this.isCountDownShow = false // 关闭倒计时
        if (e.response.status === 429) {
          this.$toast(e.response.data.message)
        } else {
          this.$toast('发送失败，请稍后重试')
        }
      }
    },
    // 返回页面
    goBack () {
      // 回跳页面
      this.$router.push(this.$route.query.path || '/')
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
      font-size: 38px;
    }
  .send-sms-btn {
    background-color: #ededed;
  }

  .send-sms-btn {
    width: 152px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
}
</style>
